/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client_GUI.SavingType;

import Client_GUI.Customer.JP_Customer;
import Client_GUI.SavingBook.JP_Deposit;
import Client_GUI.SavingBook.JP_SavingBook;
import Client_GUI.Unity;
import Interface.IRemoteCustomer;
import Interface.IRemoteSavingType;
import Server.DTO.Customer_DTO;
import Server.DTO.SavingType_DTO;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class JP_SavingType extends org.jdesktop.swingx.JXPanel {

    private Registry registry;
    private IRemoteSavingType rsavingtype;
    table_SavingType table = new table_SavingType();

    /**
     * Creates new form JP_SavingBook
     */
    public JP_SavingType() throws RemoteException, NotBoundException {
        initComponents();
        registry = LocateRegistry.getRegistry("127.0.0.1");
        rsavingtype = (IRemoteSavingType) registry.lookup("SavingType");
        table.setTable(jTable_SavingType);
        LoadTable();
    }

    public void ClearAll() {
        this.txt_MaLoai.setText("");
        checkbox_Gui.setSelected(false);
        this.txt_LaiXuat.setText("");
        this.txt_TenLoai.setText("");
        this.txt_KyHan.setText("");
        checkbox_Rut.setSelected(false);
    }

    public void LoadTable() throws RemoteException, NotBoundException {
        this.jTable_SavingType.setModel(table.LoadTable());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txt_MaLoai = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txt_TenLoai = new javax.swing.JTextField();
        txt_KyHan = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_LaiXuat = new javax.swing.JTextField();
        checkbox_Rut = new javax.swing.JCheckBox();
        checkbox_Gui = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_SavingType = new javax.swing.JTable();
        btn_Them = new javax.swing.JButton();
        btn_Sua = new javax.swing.JButton();
        btn_NhapLai = new javax.swing.JButton();
        btn_Xoa = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel1.setText("Mã Loại");

        txt_MaLoai.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        txt_MaLoai.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel2.setText("Tên Loại Tiết Kiệm");

        txt_TenLoai.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N

        txt_KyHan.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel3.setText("Kỳ Hạn");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel4.setText("Lãi Xuất");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel5.setText("Cho Phép Gửi");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel6.setText("Cho Phép Rút");

        txt_LaiXuat.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6))
                .addGap(47, 47, 47)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_MaLoai, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_TenLoai, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt_KyHan, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txt_LaiXuat, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(checkbox_Rut)
                    .addComponent(checkbox_Gui))
                .addGap(38, 38, 38))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_MaLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_TenLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txt_KyHan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_LaiXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(checkbox_Gui))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(checkbox_Rut))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jTable_SavingType.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable_SavingType.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_SavingTypeMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_SavingType);

        btn_Them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_AddCustomer.png"))); // NOI18N
        btn_Them.setText("Thêm ");
        btn_Them.setToolTipText("");
        btn_Them.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_Them.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_Them.setIconTextGap(40);
        btn_Them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ThemActionPerformed(evt);
            }
        });

        btn_Sua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_Connect.png"))); // NOI18N
        btn_Sua.setText("Sửa");
        btn_Sua.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_Sua.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_Sua.setIconTextGap(40);
        btn_Sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SuaActionPerformed(evt);
            }
        });

        btn_NhapLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_Update.png"))); // NOI18N
        btn_NhapLai.setText("Nhập lại");
        btn_NhapLai.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_NhapLai.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_NhapLai.setIconTextGap(40);
        btn_NhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_NhapLaiActionPerformed(evt);
            }
        });

        btn_Xoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_Close.png"))); // NOI18N
        btn_Xoa.setText("Xóa");
        btn_Xoa.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_Xoa.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_Xoa.setIconTextGap(40);
        btn_Xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_XoaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btn_Sua, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btn_Them, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn_Xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 846, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn_Them, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_Xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn_Sua, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn_NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 284, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTable_SavingTypeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_SavingTypeMouseClicked
        // TODO add your handling code here:
        int index = jTable_SavingType.getSelectedRow();
        this.txt_MaLoai.setText(table.getValueAt(index, 0) + "");
        this.txt_TenLoai.setText(table.getValueAt(index, 1) + "");
        this.txt_KyHan.setText(table.getValueAt(index, 2) + "");
        this.txt_LaiXuat.setText(table.getValueAt(index, 3) + "");
        if (table.getValueAt(index, 4).toString().compareToIgnoreCase("false") == 0) {
            this.checkbox_Gui.setSelected(false);
        } else {
            this.checkbox_Gui.setSelected(true);
        }
        if (table.getValueAt(index, 5).toString().compareToIgnoreCase("false") == 0) {
            this.checkbox_Rut.setSelected(false);
        } else {
            this.checkbox_Rut.setSelected(true);
        }
    }//GEN-LAST:event_jTable_SavingTypeMouseClicked

    private void btn_ThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ThemActionPerformed

        try {
            Unity.AddPanel(this, new JP_AddSavingType());
        } catch (NotBoundException ex) {
            Logger.getLogger(JP_SavingBook.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(JP_SavingBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_ThemActionPerformed

    private void btn_SuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SuaActionPerformed
        // TODO add your handling code here:
        try {
            SavingType_DTO info = new SavingType_DTO();
            info.setTypeID(txt_MaLoai.getText());
            info.setName(txt_TenLoai.getText());
            info.setTerm(Integer.parseInt(txt_KyHan.getText()));
            info.setInterestRate(Float.parseFloat(txt_LaiXuat.getText()));
            info.setAllowSave(checkbox_Gui.isSelected());
            info.setAllowWithdraw(checkbox_Rut.isSelected());
            System.out.println(txt_MaLoai.getText());
            System.out.println(txt_TenLoai.getText());
            System.out.println(Integer.parseInt(txt_KyHan.getText()));
            System.out.println(Float.parseFloat(txt_LaiXuat.getText()));
            System.out.println(checkbox_Gui.isSelected());
            System.out.println(checkbox_Rut.isSelected());
            
            Unity.AddPanel(this, new JP_UpdateSavingType(info));
        } catch (RemoteException ex) {
            Logger.getLogger(JP_Customer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(JP_Customer.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_btn_SuaActionPerformed

    private void btn_NhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_NhapLaiActionPerformed
        // TODO add your handling code here:
        ClearAll();
    }//GEN-LAST:event_btn_NhapLaiActionPerformed

    private void btn_XoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_XoaActionPerformed

        SavingType_DTO info = new SavingType_DTO();
        info.setTypeID(txt_MaLoai.getText());
        info.setName(txt_TenLoai.getText());
        info.setTerm(Integer.parseInt(txt_KyHan.getText()));

        try {

            if (!rsavingtype.Check_TypeID_Exist(info.getTypeID())) {
                rsavingtype.Delete(info);
                JOptionPane.showMessageDialog(null, "Xóa Thành Công");
                LoadTable();
            } else {
                JOptionPane.showMessageDialog(null, "Không Được Phép Xóa");
            }

        } catch (RemoteException ex) {
            Logger.getLogger(JP_SavingType.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(JP_SavingType.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_btn_XoaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_NhapLai;
    private javax.swing.JButton btn_Sua;
    private javax.swing.JButton btn_Them;
    private javax.swing.JButton btn_Xoa;
    private javax.swing.JCheckBox checkbox_Gui;
    private javax.swing.JCheckBox checkbox_Rut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_SavingType;
    private javax.swing.JTextField txt_KyHan;
    private javax.swing.JTextField txt_LaiXuat;
    private javax.swing.JTextField txt_MaLoai;
    private javax.swing.JTextField txt_TenLoai;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the registry
     */
    public Registry getRegistry() {
        return registry;
    }

    /**
     * @param registry the registry to set
     */
    public void setRegistry(Registry registry) {
        this.registry = registry;
    }

    /**
     * @return the rc
     */
    public IRemoteSavingType getRc() {
        return rsavingtype;
    }

    /**
     * @param rc the rc to set
     */
    public void setRc(IRemoteSavingType rsavingtype) {
        this.rsavingtype = rsavingtype;
    }
}
